package org.example.kidscode.service.impl;

import org.example.kidscode.entity.ReservationSetting;
import org.example.kidscode.repository.ReservationSettingRepository;
import org.example.kidscode.service.ReservationSettingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Service
@Transactional
public class ReservationSettingServiceImpl implements ReservationSettingService {

    private final ReservationSettingRepository settingRepository;

    public ReservationSettingServiceImpl(ReservationSettingRepository settingRepository) {
        this.settingRepository = settingRepository;
    }

    @Override
    public ReservationSetting saveSetting(ReservationSetting setting) {
        if (setting.getCourseId() == null || setting.getCourseId().isEmpty()) {
            throw new IllegalArgumentException("Course ID cannot be null or empty");
        }
        if (settingRepository.existsByCourseId(setting.getCourseId())) {
            throw new IllegalStateException("Course ID already exists");
        }
        return settingRepository.save(setting);
    }

    @Override
    @Transactional(readOnly = true)
    public ReservationSetting getSettingById(String settingId) {
        return settingRepository.findById(settingId)
                .orElseThrow(() -> new NoSuchElementException("Reservation setting not found with id: " + settingId));
    }

    @Override
    @Transactional(readOnly = true)
    public ReservationSetting getSettingByCourseId(String courseId) {
        return settingRepository.findByCourseId(courseId)
                .orElseThrow(() -> new NoSuchElementException("Reservation setting not found for course: " + courseId));
    }

    @Override
    @Transactional(readOnly = true)
    public List<ReservationSetting> getAllSettings() {
        return settingRepository.findAll();
    }

    @Override
    public ReservationSetting updateSetting(String settingId, ReservationSetting settingDetails) {
        ReservationSetting existing = getSettingById(settingId);

        // 使用安全更新模式，避免null覆盖
        if (settingDetails.getCourseId() != null && !settingDetails.getCourseId().equals(existing.getCourseId())) {
            if (settingRepository.existsByCourseId(settingDetails.getCourseId())) {
                throw new IllegalStateException("New course ID already exists");
            }
            existing.setCourseId(settingDetails.getCourseId());
        }
        if (settingDetails.getMaxReservations() != null) {
            existing.setMaxReservations(settingDetails.getMaxReservations());
        }
        if (settingDetails.getMinReservations() != null) {
            existing.setMinReservations(settingDetails.getMinReservations());
        }
        // 其他字段更新...

        return settingRepository.save(existing);
    }

    @Override
    public void deleteSetting(String settingId) {
        if (!settingRepository.existsById(settingId)) {
            throw new NoSuchElementException("Setting not found with id: " + settingId);
        }
        settingRepository.deleteById(settingId);
    }

    @Override
    @Transactional(readOnly = true)
    public boolean existsByCourseId(String courseId) {
        return settingRepository.existsByCourseId(courseId);
    }
}